package com.recsmile.sgs.client.listener;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.recsmile.sgs.client.card.Card;
import com.recsmile.sgs.client.commonds.sync.SyncCardDisposedGameUIClientCommand;
import com.recsmile.sgs.client.commonds.sync.SyncCardUsedGameUIClientCommand;
import com.recsmile.sgs.client.commonds.sync.SyncDisposalAreaRefreshGameUIClientCommand;
import com.recsmile.sgs.client.entity.Deck;
import com.recsmile.sgs.client.entity.GameRoom;
import com.recsmile.sgs.client.util.SyncCommandsUtil;

public class ServerInGameCardDisposalListener extends ServerInGamePlayerListener implements CardDisposalListener {
	
	private final List<Card> cards;
	private final Deck deck;

	public ServerInGameCardDisposalListener(String name, Set<String> playerNames, GameRoom room) {
		super(name, playerNames, room);
		this.cards = new ArrayList<Card>();
		this.deck = room.getGame().getDeck();
	}

	@Override
	public void onCardUsed(Card card) {
		this.cards.add(card);
		room.sendCommandToPlayers(
			SyncCommandsUtil.generateMapForSameCommand(
				name, 
				otherNames, 
				new SyncCardUsedGameUIClientCommand(name, card)
			)
		);
	}

	@Override
	public void onCardDisposed(Card card) {
		this.cards.add(card);
		room.sendCommandToPlayers(
			SyncCommandsUtil.generateMapForSameCommand(
				name, 
				otherNames, 
				new SyncCardDisposedGameUIClientCommand(name, card)
			)
		);
	}

	@Override
	public void refresh() {
		if (cards.size() > 0) {
			deck.discardAll(cards);
			cards.clear();
			room.sendCommandToPlayers(
				SyncCommandsUtil.generateMapForSameCommand(
					name, 
					otherNames, 
					new SyncDisposalAreaRefreshGameUIClientCommand()
				)
			);
		}
	}

}